<template>
  <div class="mygoods-container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 功能区域 -->
      <el-row :gutter="20">
        <!-- 根据id搜索 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="initGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="initGoodsList"></el-button>
          </el-input>
        </el-col>
        <!-- 添加用户 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <br>
      <!-- 修改商品信息对话框 -->
      <el-dialog title="修改商品信息" :visible.sync="editDialogVisible" width="50%" :before-close="handleClose">
        <el-form ref="editInfoRef" :rules="editInfoRules" :model="editInfo" label-width="100px">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="editInfo.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="editInfo.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model="editInfo.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="editInfo.goods_number"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editGoods">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 商品列表区域 -->
      <el-table :data="goodsInfo" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time|dateFormat}}
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top">
              <el-button type="primary" icon="el-icon-edit" @click="openEditDialog(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top">
              <el-button type="danger" icon="el-icon-delete" @click="deleteGoods(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>

    </el-card>
  </div>
</template>
<script>
  export default {
    name: 'MyGoods',

    data() {
      return {
        selectedId: 0,
        editInfo: {
          goods_name: '',
          goods_price: 0,
          goods_weight: 0,
          goods_number: 0
        },
        editDialogVisible: false,
        goodsInfo: [],
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 5
        },
        total: 0,
        deleteGoodsDialogVisible: false,
        editInfoRules: {
          goods_name: [{
            required: true,
            message: '请输入商品名称',
            trigger: 'blur'
          }],
          goods_price: [{
            required: true,
            message: '请输入商品价格',
            trigger: 'blur'
          }],
          goods_weight: [{
            required: true,
            message: '请输入商品重量',
            trigger: 'blur'
          }],
          goods_number: [{
            required: true,
            message: '请输入商品数量',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.initGoodsList()
    },
    methods: {
      async initGoodsList() {
        const {
          data: res
        } = await this.$http.get('goods', {
          params: this.queryInfo
        })
        if (res.meta.status !== 200) {
          this.$message({
            type: 'error',
            showClose: true,
            message: '获取数据失败',
          })
        } else {
          this.goodsInfo = res.data.goods
          this.total = res.data.total
        }
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize = val
        this.initGoodsList()

      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.queryInfo.pagenum = val
        this.initGoodsList()
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      deleteGoods(row) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {
            data: res
          } = await this.$http.delete(`goods/${row.goods_id}`)
          if (res.meta.status !== 200) {
            this.$message({
              showClose: true,
              type: 'erroe',
              message: '删除商品失败'
            })
          } else {
            this.$message({
              showClose: true,
              type: 'success',
              message: '删除商品成功'
            })
            this.initGoodsList()
          }

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      goAddGoodsPage() {
        this.$router.push('/goods/add')
      },
      openEditDialog(row) {
        this.editDialogVisible = true
        this.editInfo.goods_name = row.goods_name
        this.editInfo.goods_price = row.goods_price
        this.editInfo.goods_number = row.goods_number
        this.editInfo.goods_weight = row.goods_weight
        this.selectedId = row.goods_id
      },
      async editGoods() {
        const {
          data: res
        } = await this.$http.put(`goods/${this.selectedId}`, this.editInfo)
        if (res.meta.status !== 201) {
          this.$message({
            showClose: true,
            message: '修改商品信息失败',
            type: 'error'
          })
        } else {
          this.$message({
            showClose: true,
            message: '修改商品信息成功',
            type: 'success'
          })
          this.initGoodsList()
        }
      }
    }
  }

</script>
<style lang="less" scoped>
  .box-card {
    margin-top: 15px;
    padding: 0;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1) !important;
  }

  .el-pagination {
    margin-top: 15px
  }

</style>
